package cn.interestingshop.service.user;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.interestingshop.dao.user.UserMapper;
import cn.interestingshop.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import cn.interestingshop.entity.User;
import cn.interestingshop.utils.DataSourceUtil;

public class UserServiceImpl implements UserService {
	
	private Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	@Override
	public boolean save(User user){
		SqlSession session = MybatisUtil.openSession();
		int row = 0;
		try {
			row = session.getMapper(UserMapper.class).save(user);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			MybatisUtil.closeSession(session);
		}
		if (row>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(User user) {
		return false;
	}

	@Override
	public boolean deleteById(Integer userId) {
		return false;
	}

	@Override
	public User getById(Integer userId, String loginName) {
		User user = null;
		try {
			user = MybatisUtil.openSession().getMapper(UserMapper.class).selectById(userId,loginName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public List<User> getList(Integer currentPageNo, Integer pageSize) {
		return null;
	}

	@Override
	public int getCount() {
		return 0;
	}


//	@Override
//	public boolean update(User user) {
//		Connection connection = null;
//		Integer count=0;
//		try {
//			connection = DataSourceUtil.openConnection();
//			UserDao userDao = new UserDaoImpl(connection);
//			count=userDao.update(user);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			DataSourceUtil.closeConnection(connection);
//		}
//		return  count>0;
//	}
//
//	@Override
//	public boolean deleteById(Integer userId) {
//		Connection connection = null;
//		Integer count=0;
//		try {
//			connection = DataSourceUtil.openConnection();
//			UserDao userDao = new UserDaoImpl(connection);
//			count=userDao.deleteById(userId+"");
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			DataSourceUtil.closeConnection(connection);
//		}
//		return  count>0;
//	}
//
//	@Override
//	public User getById(Integer userId, String loginName) {
//		Connection connection = null;
//		User user=null;
//		try {
//			connection = DataSourceUtil.openConnection();
//			UserDao userDao = new UserDaoImpl(connection);
//			user=userDao.selectById(userId,loginName);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			DataSourceUtil.closeConnection(connection);
//		}
//		return user;
//	}
//
//	@Override
//	public List<User> getList(Integer currentPageNo, Integer pageSize) {
//		Connection connection = null;
//		List<User> userList=null;
//		try {
//			connection = DataSourceUtil.openConnection();
//			UserDao userDao = new UserDaoImpl(connection);
//			userList=userDao.selectList(currentPageNo,pageSize);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			DataSourceUtil.closeConnection(connection);
//		}
//		return userList;
//	}
//
//	@Override
//	public int getCount() {
//		Connection connection = null;
//		Integer count=null;
//		try {
//			connection = DataSourceUtil.openConnection();
//			UserDao userDao = new UserDaoImpl(connection);
//			count=userDao.selectCount();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			DataSourceUtil.closeConnection(connection);
//		}
//		return count;
//	}
}
